
Partial Class SAD_SQCompareL
    Inherits System.Web.UI.Page

    Dim msXMLPath As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Compare()
            'InsertSQL(1895, 0)

            '---Load servers
            Dim oServerFactory As New SQLAutoDocLib.Server_Factory
            For Each oServer As SQLAutoDocLib.Server In oServerFactory.AllActiveServers
                cmbSourceServer.Items.Add(New ListItem(oServer.Name, oServer.ID))
                cmbTargetServer.Items.Add(New ListItem(oServer.Name, oServer.ID))
            Next

            '---Load databases and/or revisions
            LoadDatabases(cmbSourceDatabase, cmbSourceServer.Value)
            LoadDatabases(cmbTargetDatabase, cmbTargetServer.Value)

            LoadRevisions(cmbSourceRevision, cmbSourceDatabase.Value)
            LoadRevisions(cmbTargetRevision, cmbTargetDatabase.Value)
        Else
            '---Load databases and/or revisions
            LoadDatabases(cmbSourceDatabase, cmbSourceServer.Value)
            LoadDatabases(cmbTargetDatabase, cmbTargetServer.Value)

            LoadRevisions(cmbSourceRevision, cmbSourceDatabase.Value)
            LoadRevisions(cmbTargetRevision, cmbTargetDatabase.Value)

            If Request.Form(cmdExecute.UniqueID) > "" Then
                Compare(cmbSourceDatabase.Value, cmbSourceRevision.Value, cmbTargetDatabase.Value, cmbTargetRevision.Value)
            End If
        End If
    End Sub

    Private Sub LoadDatabases( _
                    ByVal oControl As HtmlSelect, _
                    ByVal lServerID As Long)

        Dim sPrevID As String = Request.Form(oControl.UniqueID)

        oControl.Items.Clear()

        Dim oDatabaseFactory As New SQLAutoDocLib.Database_Factory
        For Each oDatabase As SQLAutoDocLib.Database In oDatabaseFactory.GetAllDatabasesForServer(lServerID)
            oControl.Items.Add(New ListItem(oDatabase.Name, oDatabase.ID))
        Next

        oControl.Value = sPrevID
    End Sub

    Private Sub LoadRevisions( _
                ByVal oControl As HtmlSelect, _
                ByVal sDatabaseID As String)

        Dim sPrevID As String = Request.Form(oControl.UniqueID)

        oControl.Items.Clear()

        If IsNumeric(sDatabaseID) Then
            Dim oDatabaseFactory As New SQLAutoDocLib.Database_Factory
            For Each oDatabase As SQLAutoDocLib.Database In oDatabaseFactory.GetAllRevsForDatabase(sDatabaseID)
                If oDatabase.IsWorkingCopy = False Then
                    oControl.Items.Add(New ListItem(oDatabase.RevDate, oDatabase.RevID))
                End If
            Next

            oControl.Value = sPrevID
        End If
    End Sub

    Private Sub Compare( _
                    ByVal lSourceDBID As Long, _
                    ByVal lSourceDBRevID As Long, _
                    ByVal lTargetDBID As Long, _
                    ByVal lTargetDBRevID As Long)

        Dim oDBSource As New SQLAutoDocLib.Database(lSourceDBID, lSourceDBRevID)
        oDBSource.Load()
        Dim sSourceXML As String = oDBSource.ToXML()

        Dim oDBTarget As New SQLAutoDocLib.Database(lTargetDBID, lTargetDBRevID)
        oDBTarget.Load()
        Dim sTargetXML As String = oDBTarget.ToXML()

        Dim oCOMP As New SQLAutoDocLib.CMPDatabase

        oCOMP.Compare(sSourceXML, sTargetXML)

        Dim oGUID As Guid = Guid.NewGuid
        Dim sPath As String = Application("TempPath") & oGUID.ToString & ".xml"
        Dim oText As New System.IO.StreamWriter(sPath)
        oText.Write(oCOMP.ToXML)
        oText.Flush()
        oText.Close()
        oText.Dispose()

        msXMLPath = Session("TempURL") & oGUID.ToString & ".xml"
    End Sub

    Protected Sub InsertSQL(ByVal lID As Long, ByVal lRevID As Long)
        Dim oScript As New SQLAutoDocLib.SCRIPT

        Response.Write("<PRE>")
        Response.Write(oScript.ScriptTableForInserts(lID, lRevID))
        Response.Write("</PRE>")
    End Sub

    Protected Function XMLPath() As String
        Return msXMLPath
    End Function
End Class
